﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ValidationFramework.Configuration.Fluent
{

	public class FluentAddRuleExpression<TType> : IFluentAddRuleExpression<TType>
	{
		private TypePolicyRepository _policyRepo;
		private string _ruleSet;
		/// <summary>
		/// Initializes a new instance of the FluentAddRuleExpression class.
		/// </summary>
		public FluentAddRuleExpression(TypePolicyRepository policyRepo)
		{
			_policyRepo = policyRepo;
		}

		#region IFluentRuleLocation<TType> Members

		public IRuleLocationStep<TType> AndRuleSet(string ruleSet)
		{
			return new RuleLocationStep<TType>(ResolveTypePolicy<TType>(), ruleSet);
		}

		public RuleCreationStep<TType> EnsureParentOf<TReturnType>(System.Linq.Expressions.Expression<Func<TType, TReturnType>> memberToValidate)
		{
			var locationStep = new RuleLocationStep<TType>(ResolveTypePolicy<TType>());

			return locationStep.EnsureParentOf(memberToValidate);
		}

		public RuleCreationStep<TReturnType> Ensure<TReturnType>(System.Linq.Expressions.Expression<Func<TType, TReturnType>> memberToValidate)
		{
			var locationStep = new RuleLocationStep<TType>(ResolveTypePolicy<TType>());

			return locationStep.Ensure(memberToValidate);
		}

		#endregion

		private TypePolicy ResolveTypePolicy<TType>()
		{
			return _policyRepo.GetOrCreateTypePolicy(new TypeKey(typeof(TType).TypeHandle));
		}

	}
}
